package types

// 错误定义
var (
	ErrInvalidParams       = NewError(1001, "参数错误")
	ErrPluginAlreadyLoaded = NewError(2001, "插件已加载")
	ErrPluginNotFound      = NewError(2002, "插件未找到")
	ErrLoadPluginFailed    = NewError(2003, "加载插件失败")
	ErrUnloadPluginFailed  = NewError(2004, "卸载插件失败")
	ErrGetPluginInfo       = NewError(2005, "获取插件信息失败")
	ErrCallPluginFailed    = NewError(2006, "调用插件失败")
	ErrAuthFailed          = NewError(3001, "认证失败")
	ErrGenerateToken       = NewError(3002, "生成Token失败")
)

// 请求结构体
type PluginRequest struct {
	Plugin string `path:"plugin"`
	Suffix string `path:"suffix,optional"`
}

// 请求/响应结构体
type LoadPluginReq struct {
	Name string `json:"name"`
	Path string `json:"path"`
}

type LoadPluginResp struct {
	Success bool     `json:"success"`
	Message string   `json:"message"`
	Plugin  string   `json:"plugin"`
	Version string   `json:"version"`
	Methods []string `json:"methods"`
}

type UnloadPluginReq struct {
	Name string `json:"name"`
}

type UnloadPluginResp struct {
	Success bool   `json:"success"`
	Message string `json:"message"`
	Plugin  string `json:"plugin"`
}

type ListPluginsResp struct {
	Plugins []PluginDetail `json:"plugins"`
	Count   int            `json:"count"`
}

type PluginDetail struct {
	Name        string   `json:"name"`
	Version     string   `json:"version"`
	Description string   `json:"description"`
	Methods     []string `json:"methods"`
}

type HealthCheckResp struct {
	Status map[string]HealthStatus `json:"status"`
	Time   int64                   `json:"time"`
}

type HealthStatus struct {
	Healthy bool   `json:"healthy"`
	Message string `json:"message"`
}

type LoginReq struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

type LoginResp struct {
	Success   bool     `json:"success"`
	Token     string   `json:"token"`
	ExpiresAt int64    `json:"expires_at"`
	User      UserInfo `json:"user"`
}

type UserInfo struct {
	ID       string `json:"id"`
	Username string `json:"username"`
	Role     string `json:"role"`
}

type User struct {
	ID       string
	Username string
	Password string
	Role     string
}

// 错误类型
type Error struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

func (e *Error) Error() string {
	return e.Message
}

func NewError(code int, message string) *Error {
	return &Error{
		Code:    code,
		Message: message,
	}
}
